"""
    使用 Lock锁
"""

import threading
import time
from threading import Thread, Lock

# 50张票
ticket = 50
# 创建锁对象
lock_obj = Lock()


def sale_ticket():
    global ticket
    # 每个排队窗口假设有100人
    for i in range(100):
        lock_obj.acquire()  # 上锁
        if ticket > 0:
            print(f'{threading.current_thread().name}正在出售第{ticket}张票')
            ticket -= 1
        time.sleep(1)

        lock_obj.release()  # 释放锁


if __name__ == '__main__':
    for i in range(3):
        t = Thread(target=sale_ticket)
        # 启动线程
        t.start()
